﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace WmsApp.Security
{
    internal interface IUserInfoConfigurator
    {
        void SaveUserID(string userID);
        string GetUserID();
        void SavePwd(string pwd);
        string GetPwd();
        string GetCfgValue(string key);
        void SetCfgValue(string key, string value);
    }

    public class UserInfoConfiguratorAppConfig : IUserInfoConfigurator
    {
        private const string AppName = "WmsApp.exe";
        private const string KeyUserID = "UserID";
        private const string KeyUserPwd = "UserPwd";
        #region IUserInfoConfigurator Members

        public void SaveUserID(string userID)
        {
            SetValue(KeyUserID, userID);
        }

        public string GetUserID()
        {
            return GetValue(KeyUserID);
        }

        public string GetCfgValue(string key)
        {
            return GetValue(key);
        }

        public  void SetCfgValue(string key, string value)
        {
            SetValue(key, value);
        }

        public void SavePwd(string pwd)
        {
           string val = Cryptographer.EncryptDES(pwd, Cryptographer.GetEncryptKey());
           this.SetValue(KeyUserPwd, val);
        }

        public string GetPwd()
        {
            string val = GetValue(KeyUserPwd);
            return Cryptographer.DecryptDES(val, Cryptographer.GetEncryptKey());
        }

        #region private function
        /// <summary>
        /// 设置app.config中的某个key的value.
        /// </summary>
        /// <param name="appKey">key</param>
        /// <param name="appValue">value</param>
        private void SetValue(string appKey, string appValue)
        {
            var xDoc = new XmlDocument();

            //此处配置文件在程序目录下
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + AppName;
            xDoc.Load(strFileName + ".config");
            
            var xNode = xDoc.SelectSingleNode("//appSettings");

            if (xNode == null)
            {
                var newNode = xDoc.CreateNode(XmlNodeType.Element, "appSettings", String.Empty);
                if (xDoc.DocumentElement != null)
                {
                    xDoc.DocumentElement.AppendChild(newNode);
                    xNode = xDoc.SelectSingleNode("//appSettings");
                }
                else
                {
                    return;
                }
            }

            var xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
            if (xElem1 != null)
            {
                xElem1.SetAttribute("value", appValue);
            }
            else
            {
                var xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("key", appKey);
                xElem2.SetAttribute("value", appValue);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(strFileName + ".config");
        }

        private string GetValue(string appKey)
        {
            var xDoc = new XmlDocument();

            //此处配置文件在程序目录下
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + AppName;
            xDoc.Load(strFileName + ".config");
            var xNode = xDoc.SelectSingleNode("//appSettings");
            if(xNode == null)
            {
                return "";
            }
            var xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
            if (xElem1 != null)
            {
                return xElem1.GetAttribute("value");
            }
            return @"";
        }
        #endregion

        #endregion
    }
}
